package com.zpz.framework.zpzoauth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthPageInfoExt;
import com.zpz.framework.zpzoauth.dao.FrameDepartmentMapper;
import com.zpz.framework.zpzoauth.pojo.pe.GetDepartmentListPe;
import com.zpz.framework.zpzoauth.pojo.po.FrameDepartment;
import com.zpz.framework.zpzoauth.pojo.qo.*;
import com.zpz.framework.zpzoauth.pojo.vo.GetDepartmentPageVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameDepartmentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 */
@Service
public class ZpzOauthFrameDepartmentServiceImpl implements ZpzOauthFrameDepartmentService {
    private Logger log = LoggerFactory.getLogger(ZpzOauthFrameDepartmentServiceImpl.class);
    @Resource
    private FrameDepartmentMapper frameDepartmentMapper;
    /***
     * 获取部门
     * @name: ZpzOauthFrameDepartmentServiceImpl
     * @author: zhangpanzhi
     * @createDate: 2020/10/13 15:28
     * @param getDepartmentPageQo getDepartmentPageQo
     * @return: ZpzOauthResultMsg<com.github.pagehelper.PageInfo < GetDepartmentPageVo>>
     * @throws:
     */
    @Override
    public ZpzOauthResultMsg<PageInfo<GetDepartmentPageVo>> getDepartmentPage(GetDepartmentPageQo getDepartmentPageQo) {
        if (StringUtils.isNotBlank(getDepartmentPageQo.getDepartmentName())){
            if (getDepartmentPageQo.getDepartmentName().equals("%")){
                getDepartmentPageQo.setDepartmentName("％");
            } else if (getDepartmentPageQo.getDepartmentName().equals("_")){
                getDepartmentPageQo.setDepartmentName("＿");
            }
        }
        PageHelper.startPage(getDepartmentPageQo).setOrderBy("create_date desc");
        List<GetDepartmentListPe> list = frameDepartmentMapper.selectDepartmentList(getDepartmentPageQo.getDepartmentName());
        PageInfo<GetDepartmentPageVo> page = new ZpzOauthPageInfoExt<GetDepartmentListPe,GetDepartmentPageVo>().getPage(list,GetDepartmentPageVo.class);
        return ZpzOauthResultMsg.okForData(page);
    }

    @Override
    public ZpzOauthResultMsg<Boolean> disableDepartment(DisableDepartmentQo disableDepartmentQo) {
        FrameDepartment fd=new FrameDepartment();
        fd.setStatus(disableDepartmentQo.getStatus());
        fd.setId(disableDepartmentQo.getId());
        fd.setUpdateBy(disableDepartmentQo.getUpdateBy());
        fd.setUpdateDate(new Date());
        int i = frameDepartmentMapper.updateDepartment(fd);
        if (i>0){
            return ZpzOauthResultMsg.okForData(true);
        }
        return ZpzOauthResultMsg.error();
    }

    @Override
    public ZpzOauthResultMsg<Boolean> removeDepartment(RemoveDepartmentQo removeDepartmentQo) {
        FrameDepartment fd=new FrameDepartment();
        fd.setDelFlag(1);
        fd.setId(removeDepartmentQo.getId());
        fd.setUpdateBy(removeDepartmentQo.getUpdateBy());
        fd.setUpdateDate(new Date());
        int i = frameDepartmentMapper.updateDepartment(fd);
        if (i>0){
            return ZpzOauthResultMsg.okForData(true);
        }
        return ZpzOauthResultMsg.error();
    }

    @Override
    public ZpzOauthResultMsg<GetDepartmentPageVo> getDepartmentInfo(GetDepartmentInfoQo getDepartmentInfoQo) {
        FrameDepartment fd=new FrameDepartment();
        fd.setDelFlag(0);
        fd.setId(getDepartmentInfoQo.getId());
        List<FrameDepartment> list = frameDepartmentMapper.select(fd);
        if (list!=null&&list.size()>0){
            FrameDepartment frameDepartment = list.get(0);
            GetDepartmentPageVo gdp=new GetDepartmentPageVo();
            BeanUtils.copyProperties(frameDepartment,gdp);
            return ZpzOauthResultMsg.okForData(gdp);
        }
        return ZpzOauthResultMsg.error();
    }


    @Override
    public ZpzOauthResultMsg<Boolean> addDepartment(AddDepartmentQo addDepartmentQo) {
        FrameDepartment fd=new FrameDepartment();
        BeanUtils.copyProperties(addDepartmentQo,fd);
        fd.setCreateDate(new Date());
        fd.setStatus(0);
        fd.setDelFlag(0);
        int insert = frameDepartmentMapper.insert(fd);
        if (insert>0){
            return ZpzOauthResultMsg.okForData(true);
        }
        return ZpzOauthResultMsg.error();
    }
}
